#include <msp430f6638.h>
#include "stdint.h"

//#define CPU_F ((double)1000000)
//#define delay_us(x) __delay_cycles((long)CPU_F*(double)x/1000000.0)
//#define delay_ms(x) __delay_cycles((long)CPU_F*(double)x/1000.0)

#define NumOfResult 8
#define Max_Volt 3.3
#define Accu_Adc 4095

uint32_t results[NumOfResult+1];
uint16_t average;
uint16_t index = 0;

#define XT2_PORT_SEL P7SEL
#define XT2_ENABLE (BIT2+BIT3)



#define d 0x01
#define c 0x20
#define b 0x40
#define a 0x80
#define dp 0x10
#define g 0x04
#define f 0x08
#define e 0x02

const char char_gen[] = {

                         a+b+c+d+e+f,   //0
                         b+c,           //1
                         a+b+d+e+g,     //2
                         a+b+c+d+g,     //3
                         b+c+f+g,       //4
                         a+c+d+f+g,      //5
                         a+c+d+e+f+g,   //6
                         a+b+c,         //7
                         a+b+c+d+e+f+g, //8
                         a+b+c+d+f+g,   //9

};
uint8_t con[10], trans_v[10];
void Init_lcd(void);
void LcdGo(unsigned char Dot);
void LcdBlink(unsigned char doit);
void LCD_Clear(void);
void Init_TS3A5017DR(void);
void Backlight_Enable(void);


void Init_lcd(void){
    LCDBCTL0 = LCDDIV0 + LCDPRE0 + LCDMX1 + LCDSSEL + LCDMX1 + LCD4MUX;
    LCDBPCTL0 = LCDS0 + LCDS1 + LCDS2 + LCDS3 + LCDS4 + LCDS5 + LCDS6 + LCDS7 + LCDS8 + LCDS9 + LCDS10 + LCDS11;
    P5SEL = 0xfc;
}

void LcdGo(unsigned char Dot){
    if(Dot == 1){
        LCDBCTL0 |= LCDON;
    }
    else if(Dot == 1){
        LCDBCTL0 &= ~LCDON;
    }
}


void LcdBlink(unsigned char doit){
    if(doit == 0){
        LCDBCTL0 &= ~LCDSON;
    }
    else if(doit == 1){
        LCDBCTL0 |= LCDSON;
    }
}

void LCD_Clear(void){
    unsigned char index;
    for(index = 0; index < 12; index++){
        LCDMEM[index] = 0;
    }
}



void Init_TS3A5017DR(void){
    P1DIR |= BIT6 + BIT7;
    P1OUT &= ~BIT7;
    P1OUT |= BIT6;

}



void Backlight_Enable(void){
    P8DIR |= BIT0;
    P8OUT |= BIT0;
}

void Backlight_Disable(void){
    P8DIR |= BIT0;
    P8OUT &= ~BIT0;
}

int main(void){
    WDTCTL = WDTPW+WDTHOLD;
    Init_TS3A5017DR();
    Init_lcd();
    LcdGo(1);
    LCD_Clear();
    Backlight_Enable();
    P6SEL |= BIT6;


    ADC12CTL0 = ADC12ON + ADC12SHT0_15 + ADC12MSC;
    ADC12CTL1 = ADC12SHP + ADC12CONSEQ_2 + ADC12SSEL_1;

    ADC12MCTL0 |= ADC12INCH_6;
    ADC12IE = ADC12IE0;
    ADC12CTL0 |= ADC12ENC;
    ADC12CTL0 |= ADC12SC;
    _BIS_SR(LPM4_bits + GIE);

}

#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR(void){
    results[index++] = ADC12MEM0;
    if(index == NumOfResult){
        uint16_t i;
        average = 0;

        for(i=0; i < NumOfResult; i++){
            average += results[i];
            results[i] = 0;
        }

        average >>= 3;
        index = 0;


        average=average*3.3*1000/4095;


        LCDMEM[2] = char_gen[average/1000];
        LCDMEM[3] = char_gen[average/100%10];
        LCDMEM[4] = char_gen[average/10%10];
        LCDMEM[5] = char_gen[average%10];
        LCDMEM[3] |= 0x10;
    }
}
